package com.jackchong.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.jackchong.utils.JAuto;;
import com.jackchong.utils.ViewUtils;
import com.utils.R;

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/8/15 14:36
 */

public class SwitchView extends View implements View.OnClickListener{

    private boolean mEnabled;
    private Paint mPaint;
    private RectF mRectF;
    private ValueAnimator mAnimator;
    private float mValue;
    private Paint mCirclePaint;
    private float mOffsetX;
    private boolean mIntercept;
    private OnSwitchStateChangeListener mOnSwitchStateChangeListener;
    private int mOnBgcolor;
    private int mOffBgcolor;
    private int mBtnColor;
    private int mTextColor;
    private String mOnText;
    private String mOffText;
    private float mTextSize;
    private TextPaint mTextPaint;
    private float mPadding;
    private int mCenterX;
    private int mCenterY;

    public SwitchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchView);
        parseAttrs(typedArray);
        typedArray.recycle();
        init();
    }

    private void parseAttrs(TypedArray typedArray) {
        mOnBgcolor = typedArray.getColor(R.styleable.SwitchView_JONBgColor, 0xFF1392FD);
        mOffBgcolor = typedArray.getColor(R.styleable.SwitchView_JOFFBgColor, 0xFFCECECE);
        mBtnColor = typedArray.getColor(R.styleable.SwitchView_JBtnColor, Color.WHITE);
        mTextColor = typedArray.getColor(R.styleable.SwitchView_JTextColor, Color.BLACK);
        mOnText = typedArray.getString(R.styleable.SwitchView_JONText);
        mOffText = typedArray.getString(R.styleable.SwitchView_JOFFText);
        mTextSize = JAuto.auto(typedArray.getDimension(R.styleable.SwitchView_JTextSize, 22));
        mPadding = JAuto.auto(typedArray.getDimension(R.styleable.SwitchView_JPadding, 3));
        mEnabled = typedArray.getBoolean(R.styleable.SwitchView_JEnable, false);
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mRectF = new RectF();

        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mBtnColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);

        initAnim();
        setOnClickListener(this);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterX = w / 2;
        mCenterY = h / 2;
    }

    private void initAnim() {
        mAnimator = ValueAnimator.ofFloat(0, 1);
        mAnimator.setDuration(100);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.addUpdateListener(mAnimatorUpdateListener);
        mAnimator.addListener(mAnimatorListener);
    }

    private ValueAnimator.AnimatorUpdateListener mAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            mValue = animation.getAnimatedFraction();
            postInvalidate();
        }
    };

    private ValueAnimator.AnimatorListener mAnimatorListener = new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            mEnabled = !mEnabled;
            mValue = 0;
        }
    };

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawRect(canvas);

        drawCircle(canvas);
    }


    private void drawRect(Canvas canvas) {
        mPaint.setColor(mOffBgcolor);
        mRectF.set(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(mRectF, mRectF.centerY(), mRectF.centerY(), mPaint);
        if (mOffText != null){
            canvas.drawText(mOffText, getWidth() - (getWidth() - getHeight()) / 2, ViewUtils.getTextCenterY(mCenterY, mTextPaint), mTextPaint);
        }

        mOffsetX = mEnabled ? getWidth() - (getWidth() - getHeight()) * (mValue) - mCenterY : mCenterY + (getWidth() - getHeight()) * mValue;
        if (mOffsetX != mCenterY){
            mPaint.setColor(mOnBgcolor);
            mRectF.right = mOffsetX + mCenterY;
            canvas.drawRoundRect(mRectF, mRectF.centerY(), mRectF.centerY(), mPaint);

            if (mOnText != null){
                canvas.drawText(mOnText, (getWidth() - getHeight()) / 2, ViewUtils.getTextCenterY(mCenterY, mTextPaint), mTextPaint);
            }
        }
    }

    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(mOffsetX, mCenterY, mCenterY - mPadding, mCirclePaint);
    }



    public void setEnabled(boolean enabled){
        //取反, 因为动画运行是反的
        if (isEnabled() != enabled) {
            mEnabled = !enabled;
            mAnimator.start();
        }
    }

    public boolean isEnabled(){
        return mEnabled;
    }

    @Override
    public void onClick(View v) {
        if (!mAnimator.isRunning() && !mIntercept) {
            mAnimator.start();
            if (mOnSwitchStateChangeListener != null){
                mOnSwitchStateChangeListener.onChange(!mEnabled);
            }
        }
    }

    public void setInterceptClick(boolean intercept){
        mIntercept = intercept;
    }

    public interface OnSwitchStateChangeListener{
        void onChange(boolean state);
    }

    public void setOnSwitchStateChangeListener(@Nullable SwitchView.OnSwitchStateChangeListener listener){
        mOnSwitchStateChangeListener = listener;
    }

}
